<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
   var combine = function (n, k) {
      let result = [], path = [];
      const backtracking = (n, k, startIndex) => {
        if (path.length == k) {
          result.push([...path]);
          return;
        }

        for (let i = startIndex; i <= n; i++) {
          path.push(i);
          backtracking(n, k, i + 1);
          path.pop();
        }
      }

      backtracking(n, k, 1);
      return result;
    };

    console.log(combine(4, 2));
  </script>
</body>
</html>